package com.itzdm.message.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itzdm.message.domain.po.Message;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @作者：张德明
 * @时间：2025/4/13 15:07
 */
@Mapper
public interface MessageMapper extends BaseMapper<Message> {

    /**
     * 分页查询两个用户之间的聊天记录
     */
    @Select("SELECT m.* FROM message m " +
            "JOIN user_message_relation umr1 ON m.id = umr1.message_id AND umr1.user_id = #{userId} AND umr1.is_deleted = 0 " +
            "JOIN user_message_relation umr2 ON m.id = umr2.message_id AND umr2.user_id = #{friendId} AND umr2.is_deleted = 0 " +
            "ORDER BY m.create_time DESC")
    IPage<Message> selectChatMessages(Page<Message> page, @Param("userId") Long userId, @Param("friendId") Long friendId);
}
